#!/bin/bash

if [ -z "$HOME" ]
then
  mkdir -p $SNAP_DATA/var/tmp/
  export HOME=$SNAP_DATA/var/tmp/
fi

set -eu

export PATH="$SNAP/usr/sbin:$SNAP/usr/bin:$SNAP/sbin:$SNAP/bin:$PATH"
ARCH="$($SNAP/bin/uname -m)"
export LD_LIBRARY_PATH="$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/$ARCH-linux-gnu:$SNAP/usr/lib/$ARCH-linux-gnu"
export LD_LIBRARY_PATH=$SNAP_LIBRARY_PATH:$LD_LIBRARY_PATH
export OPENSSL_CONF="/snap/microk8s/current/etc/ssl/openssl.cnf"

# This is really the only way I could find to get the args passed in correctly. WTF
declare -a args="($(cat $SNAP_DATA/args/cluster-agent))"

cd ${SNAP}/scripts/
${SNAP}/usr/bin/gunicorn3 cluster.agent:app "${args[@]}"
